package com.zatech.cgnci.project.controller;


import cn.hutool.poi.excel.ExcelWriter;
import com.zatech.cgnci.project.base.enums.ResponseCodeEnum;
import com.zatech.cgnci.project.base.exception.GlobalException;
import com.zatech.cgnci.project.dto.ClaimsFileDTO;
import com.zatech.cgnci.project.service.CgnciOutstandingClaimsReserveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 未决赔款准备金评估表 前端控制器
 * </p>
 *
 * @author Generator
 * @since 2025-01-20
 */
@RestController
@RequestMapping("/cgnciOutstandingClaimsReserve")
@Api(produces = "application/json", tags = "未决赔款准备金评估假设模版api接口文档")
public class CgnciOutstandingClaimsReserveController {

    @Autowired
    private CgnciOutstandingClaimsReserveService claimsReserveService;


    @PostMapping("/import")
    @ApiOperation(value = "导入假设模版")
    public void importClaimsReserve(@Valid ClaimsFileDTO file){
        claimsReserveService.importClaimsReserve(file.getFile());
    }

    /**
     * 导出列表
     */
    @GetMapping("/exportAll")
    @ApiOperation(value = "导出假设模版，true为查询数据导出")
    public void exportProjectAll(HttpServletResponse response,Boolean derivedData){
        try(OutputStream outputStream = response.getOutputStream();
            ExcelWriter writer = claimsReserveService.exportAll(derivedData);) {
            //response.setContentType("application/x-download");
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            String fileName = URLEncoder.encode("未决赔款准备金评估假设模版.xlsx", "UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            writer.getWorkbook().write(outputStream);
            response.getOutputStream().flush();
        }catch (Exception e){
            throw new GlobalException(ResponseCodeEnum.PARAMS_ILLEGAL, "下载模板失败");
        }
    }
}

